package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.ModifyTimeJob;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIWrite;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;
import java.util.UUID;

/**
 * @author LiangTianQi
 * @describe
 * @date 2020/11/13 15:53
 */
@RestController
@RequestMapping("/cron")
public class CronController {

    @Reference
    private OrderSettingService orderSettingService;

    //远程注入定时器的修改
    @Reference
    private ModifyTimeJob modifyTimeJob;

    /*public void method() {
        System.out.println("执行定时任务: " + LocalDateTime.now().toLocalTime());
    }*/

    @RequestMapping("/time")
    public Result updateCron(String number) {
        try {
            //orderSettingService.updateCron(number);
            //将从前端获取的时间，拼接成cron
            String cron = "0/" + number + " * * * * ?";
            modifyTimeJob.modifyJobTime(cron);
            return new Result(true, "更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "更新失败");
        }

    }

    @RequestMapping("/xlsx_get/{date}")
    public void getImage(@PathVariable String date,
            HttpServletRequest request,
            HttpServletResponse response) {
        //文件输入流
        FileInputStream is=null;
        FileOutputStream out = null;
        //referer记录了该请求是从哪个链接过来的，可以用来判断请求是否合法

        String imageDirectory = request.getRealPath("/template");

        //从数据库中查询数据
        List<OrderSetting> list = orderSettingService.getOrdersettingByMonth(date);

        //使用poi将数据封装进表格中
//        File file = new File(imageDirectory,"order.xlsx");
        String string = UUID.randomUUID().toString();
        File file1 = new File(imageDirectory,string+".xlsx");
        try {
//            is = new FileInputStream(file);
            out = new FileOutputStream(file1);
            POIWrite.write(list,out);
        } catch (Exception e) {
            e.printStackTrace();
        }


        if (file1.exists()) {
//                response.setContentType("image/jpeg");
            response.setHeader("Content-Type", "application/octet-stream");
            response.setHeader("Content-Disposition", "attachment;filename="+string+".xlsx");
            //不设置Content-Disposition的，图像在浏览器内打开
            byte[] buffer = new byte[1024];
            FileInputStream fis = null;
            BufferedInputStream bis = null;
            // if you're using Java 7, use try-with-resources
            try {
                fis = new FileInputStream(file1);
                bis = new BufferedInputStream(fis);
                OutputStream os = response.getOutputStream();
                int i = bis.read(buffer);
                while (i != -1) {
                    os.write(buffer, 0, i);
                    i = bis.read(buffer);
                }
            } catch (IOException ex) {
                // do something here
            } finally {
                if (bis != null) {
                    try {
                        bis.close();
                    } catch (IOException e) {

                    }
                }
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (IOException e) {

                    }
                }
                if (is != null) {
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }



}
